{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 12.11 \u5b9e\u73b0\u6d88\u606f\u53d1\u5e03/\u8ba2\u9605\u6a21\u578b\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u6709\u4e00\u4e2a\u57fa\u4e8e\u7ebf\u7a0b\u901a\u4fe1\u7684\u7a0b\u5e8f\uff0c\u60f3\u8ba9\u5b83\u4eec\u5b9e\u73b0\u53d1\u5e03/\u8ba2\u9605\u6a21\u5f0f\u7684\u6d88\u606f\u901a\u4fe1\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8981\u5b9e\u73b0\u53d1\u5e03/\u8ba2\u9605\u7684\u6d88\u606f\u901a\u4fe1\u6a21\u5f0f\uff0c\n\u4f60\u901a\u5e38\u8981\u5f15\u5165\u4e00\u4e2a\u5355\u72ec\u7684\u201c\u4ea4\u6362\u673a\u201d\u6216\u201c\u7f51\u5173\u201d\u5bf9\u8c61\u4f5c\u4e3a\u6240\u6709\u6d88\u606f\u7684\u4e2d\u4ecb\u3002\n\u4e5f\u5c31\u662f\u8bf4\uff0c\u4e0d\u76f4\u63a5\u5c06\u6d88\u606f\u4ece\u4e00\u4e2a\u4efb\u52a1\u53d1\u9001\u5230\u53e6\u4e00\u4e2a\uff0c\u800c\u662f\u5c06\u5176\u53d1\u9001\u7ed9\u4ea4\u6362\u673a\uff0c\n\u7136\u540e\u7531\u4ea4\u6362\u673a\u5c06\u5b83\u53d1\u9001\u7ed9\u4e00\u4e2a\u6216\u591a\u4e2a\u88ab\u5173\u8054\u4efb\u52a1\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684\u4ea4\u6362\u673a\u5b9e\u73b0\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from collections import defaultdict\n\nclass Exchange:\n    def __init__(self):\n        self._subscribers = set()\n\n    def attach(self, task):\n        self._subscribers.add(task)\n\n    def detach(self, task):\n        self._subscribers.remove(task)\n\n    def send(self, msg):\n        for subscriber in self._subscribers:\n            subscriber.send(msg)\n\n# Dictionary of all created exchanges\n_exchanges = defaultdict(Exchange)\n\n# Return the Exchange instance associated with a given name\ndef get_exchange(name):\n    return _exchanges[name]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u4e2a\u4ea4\u6362\u673a\u5c31\u662f\u4e00\u4e2a\u666e\u901a\u5bf9\u8c61\uff0c\u8d1f\u8d23\u7ef4\u62a4\u4e00\u4e2a\u6d3b\u8dc3\u7684\u8ba2\u9605\u8005\u96c6\u5408\uff0c\u5e76\u4e3a\u7ed1\u5b9a\u3001\u89e3\u7ed1\u548c\u53d1\u9001\u6d88\u606f\u63d0\u4f9b\u76f8\u5e94\u7684\u65b9\u6cd5\u3002\n\u6bcf\u4e2a\u4ea4\u6362\u673a\u901a\u8fc7\u4e00\u4e2a\u540d\u79f0\u5b9a\u4f4d\uff0cget_exchange() \u901a\u8fc7\u7ed9\u5b9a\u4e00\u4e2a\u540d\u79f0\u8fd4\u56de\u76f8\u5e94\u7684 Exchange \u5b9e\u4f8b\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u4f8b\u5b50\uff0c\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u4e00\u4e2a\u4ea4\u6362\u673a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Example of a task.  Any object with a send() method\n\nclass Task:\n    ...\n    def send(self, msg):\n        ...\n\ntask_a = Task()\ntask_b = Task()\n\n# Example of getting an exchange\nexc = get_exchange('name')\n\n# Examples of subscribing tasks to it\nexc.attach(task_a)\nexc.attach(task_b)\n\n# Example of sending messages\nexc.send('msg1')\nexc.send('msg2')\n\n# Example of unsubscribing\nexc.detach(task_a)\nexc.detach(task_b)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5c3d\u7ba1\u5bf9\u4e8e\u8fd9\u4e2a\u95ee\u9898\u6709\u5f88\u591a\u7684\u53d8\u79cd\uff0c\u4e0d\u8fc7\u4e07\u53d8\u4e0d\u79bb\u5176\u5b97\u3002\n\u6d88\u606f\u4f1a\u88ab\u53d1\u9001\u7ed9\u4e00\u4e2a\u4ea4\u6362\u673a\uff0c\u7136\u540e\u4ea4\u6362\u673a\u4f1a\u5c06\u5b83\u4eec\u53d1\u9001\u7ed9\u88ab\u7ed1\u5b9a\u7684\u8ba2\u9605\u8005\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u901a\u8fc7\u961f\u5217\u53d1\u9001\u6d88\u606f\u7684\u4efb\u52a1\u6216\u7ebf\u7a0b\u7684\u6a21\u5f0f\u5f88\u5bb9\u6613\u88ab\u5b9e\u73b0\u5e76\u4e14\u4e5f\u975e\u5e38\u666e\u904d\u3002\n\u4e0d\u8fc7\uff0c\u4f7f\u7528\u53d1\u5e03/\u8ba2\u9605\u6a21\u5f0f\u7684\u597d\u5904\u66f4\u52a0\u660e\u663e\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u9996\u5148\uff0c\u4f7f\u7528\u4e00\u4e2a\u4ea4\u6362\u673a\u53ef\u4ee5\u7b80\u5316\u5927\u90e8\u5206\u6d89\u53ca\u5230\u7ebf\u7a0b\u901a\u4fe1\u7684\u5de5\u4f5c\u3002\n\u65e0\u9700\u53bb\u5199\u901a\u8fc7\u591a\u8fdb\u7a0b\u6a21\u5757\u6765\u64cd\u4f5c\u591a\u4e2a\u7ebf\u7a0b\uff0c\u4f60\u53ea\u9700\u8981\u4f7f\u7528\u8fd9\u4e2a\u4ea4\u6362\u673a\u6765\u8fde\u63a5\u5b83\u4eec\u3002\n\u67d0\u79cd\u7a0b\u5ea6\u4e0a\uff0c\u8fd9\u4e2a\u5c31\u8ddf\u65e5\u5fd7\u6a21\u5757\u7684\u5de5\u4f5c\u539f\u7406\u7c7b\u4f3c\u3002\n\u5b9e\u9645\u4e0a\uff0c\u5b83\u53ef\u4ee5\u8f7b\u677e\u7684\u89e3\u8026\u7a0b\u5e8f\u4e2d\u591a\u4e2a\u4efb\u52a1\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5176\u6b21\uff0c\u4ea4\u6362\u673a\u5e7f\u64ad\u6d88\u606f\u7ed9\u591a\u4e2a\u8ba2\u9605\u8005\u7684\u80fd\u529b\u5e26\u6765\u4e86\u4e00\u4e2a\u5168\u65b0\u7684\u901a\u4fe1\u6a21\u5f0f\u3002\n\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u591a\u4efb\u52a1\u7cfb\u7edf\u3001\u5e7f\u64ad\u6216\u6247\u51fa\u3002\n\u4f60\u8fd8\u53ef\u4ee5\u901a\u8fc7\u4ee5\u666e\u901a\u8ba2\u9605\u8005\u8eab\u4efd\u7ed1\u5b9a\u6765\u6784\u5efa\u8c03\u8bd5\u548c\u8bca\u65ad\u5de5\u5177\u3002\n\u4f8b\u5982\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u8bca\u65ad\u7c7b\uff0c\u53ef\u4ee5\u663e\u793a\u88ab\u53d1\u9001\u7684\u6d88\u606f\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class DisplayMessages:\n    def __init__(self):\n        self.count = 0\n    def send(self, msg):\n        self.count += 1\n        print('msg[{}]: {!r}'.format(self.count, msg))\n\nexc = get_exchange('name')\nd = DisplayMessages()\nexc.attach(d)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\uff0c\u8be5\u5b9e\u73b0\u7684\u4e00\u4e2a\u91cd\u8981\u7279\u70b9\u662f\u5b83\u80fd\u517c\u5bb9\u591a\u4e2a\u201ctask-like\u201d\u5bf9\u8c61\u3002\n\u4f8b\u5982\uff0c\u6d88\u606f\u63a5\u53d7\u8005\u53ef\u4ee5\u662factor\uff0812.10\u5c0f\u8282\u4ecb\u7ecd\uff09\u3001\u534f\u7a0b\u3001\u7f51\u7edc\u8fde\u63a5\u6216\u4efb\u4f55\u5b9e\u73b0\u4e86\u6b63\u786e\u7684 send() \u65b9\u6cd5\u7684\u4e1c\u897f\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5173\u4e8e\u4ea4\u6362\u673a\u7684\u4e00\u4e2a\u53ef\u80fd\u95ee\u9898\u662f\u5bf9\u4e8e\u8ba2\u9605\u8005\u7684\u6b63\u786e\u7ed1\u5b9a\u548c\u89e3\u7ed1\u3002\n\u4e3a\u4e86\u6b63\u786e\u7684\u7ba1\u7406\u8d44\u6e90\uff0c\u6bcf\u4e00\u4e2a\u7ed1\u5b9a\u7684\u8ba2\u9605\u8005\u5fc5\u987b\u6700\u7ec8\u8981\u89e3\u7ed1\u3002\n\u5728\u4ee3\u7801\u4e2d\u901a\u5e38\u4f1a\u662f\u50cf\u4e0b\u9762\u8fd9\u6837\u7684\u6a21\u5f0f\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "exc = get_exchange('name')\nexc.attach(some_task)\ntry:\n    ...\nfinally:\n    exc.detach(some_task)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u67d0\u79cd\u610f\u4e49\u4e0a\uff0c\u8fd9\u4e2a\u548c\u4f7f\u7528\u6587\u4ef6\u3001\u9501\u548c\u7c7b\u4f3c\u5bf9\u8c61\u5f88\u50cf\u3002\n\u901a\u5e38\u5f88\u5bb9\u6613\u4f1a\u5fd8\u8bb0\u6700\u540e\u7684 detach() \u6b65\u9aa4\u3002\n\u4e3a\u4e86\u7b80\u5316\u8fd9\u4e2a\uff0c\u4f60\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u534f\u8bae\u3002\n\u4f8b\u5982\uff0c\u5728\u4ea4\u6362\u673a\u5bf9\u8c61\u4e0a\u589e\u52a0\u4e00\u4e2a subscribe() \u65b9\u6cd5\uff0c\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from contextlib import contextmanager\nfrom collections import defaultdict\n\nclass Exchange:\n    def __init__(self):\n        self._subscribers = set()\n\n    def attach(self, task):\n        self._subscribers.add(task)\n\n    def detach(self, task):\n        self._subscribers.remove(task)\n\n    @contextmanager\n    def subscribe(self, *tasks):\n        for task in tasks:\n            self.attach(task)\n        try:\n            yield\n        finally:\n            for task in tasks:\n                self.detach(task)\n\n    def send(self, msg):\n        for subscriber in self._subscribers:\n            subscriber.send(msg)\n\n# Dictionary of all created exchanges\n_exchanges = defaultdict(Exchange)\n\n# Return the Exchange instance associated with a given name\ndef get_exchange(name):\n    return _exchanges[name]\n\n# Example of using the subscribe() method\nexc = get_exchange('name')\nwith exc.subscribe(task_a, task_b):\n     ...\n     exc.send('msg1')\n     exc.send('msg2')\n     ...\n\n# task_a and task_b detached here"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\u8fd8\u5e94\u8be5\u6ce8\u610f\u7684\u662f\u5173\u4e8e\u4ea4\u6362\u673a\u7684\u601d\u60f3\u6709\u5f88\u591a\u79cd\u7684\u6269\u5c55\u5b9e\u73b0\u3002\n\u4f8b\u5982\uff0c\u4ea4\u6362\u673a\u53ef\u4ee5\u5b9e\u73b0\u4e00\u6574\u4e2a\u6d88\u606f\u901a\u9053\u96c6\u5408\u6216\u63d0\u4f9b\u4ea4\u6362\u673a\u540d\u79f0\u7684\u6a21\u5f0f\u5339\u914d\u89c4\u5219\u3002\n\u4ea4\u6362\u673a\u8fd8\u53ef\u4ee5\u88ab\u6269\u5c55\u5230\u5206\u5e03\u5f0f\u8ba1\u7b97\u7a0b\u5e8f\u4e2d\uff08\u6bd4\u5982\uff0c\u5c06\u6d88\u606f\u8def\u7531\u5230\u4e0d\u540c\u673a\u5668\u4e0a\u9762\u7684\u4efb\u52a1\u4e2d\u53bb\uff09\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}